<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>03计算器练习</title>
</head>
<body>
<input type="text" placeholder="请输入数字1" id="i1"><br>
<input type="text" placeholder="请输入数字2" id="i2"><br>
<button onclick="calc('+')">加</button>
<button onclick="calc('-')">减</button>
<button onclick="calc('*')">乘</button>
<button onclick="calc('/')">除</button>
<h4>运算结果:<span></span></h4>
<script>
  function calc(o){
    let input1 = document.querySelector('#i1')
    let input2 = document.getElementById('i2')
    console.log(input1.value,typeof input1.value)
    console.log(input2.value,typeof input2.value)
    let n1 = parseFloat(input1.value)
    let n2 = parseFloat(input2.value)
    console.log(i1,typeof i1)
    console.log(i2,typeof i2)
    let spanE = document.querySelector('span')
    switch (o){
      case '+':
        spanE.innerHTML = n1+n2;break
      case '-':
        spanE.innerHTML = n1-n2;break
      case '*':
        spanE.innerHTML = n1*n2;break
      case '/':
            if(n2!=0){
                spanE.innerHTML = n1/n2;
            }else{
                alert('除数不能为0!');
            }
        }
  }
</script>
</body>
</html>